<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
		<link rel="stylesheet" type="text/css" href="../css/aui.css"/>
		<link rel="stylesheet" type="text/css" href="../icon/aosen/css/font-awesome.min.css" />
    <style>
    	body,html{
				background:white;
    	}

    </style>
</head>
<body>
    <div class="aui-content aui-margin-b-15">
        <ul class="aui-list aui-list-in" id="list">

        </ul>
				<ul class="aui-list aui-media-list" id="messageList">

          </ul>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/request.js"></script>
<script type="text/javascript">
var rong;
var userId;
var userHeader;
var userName;
	apiready = function(){
		api.addEventListener({
	        name:'backFchatroom'
        },function(ret,err){
        	window.location.reload();
        });
		userId = $api.getStorage('userId');
		userHeader = $api.getStorage('userHeader');
		userName = $api.getStorage('userName');
			getMsgList();
			rong = api.require('rongCloud2');
	    rong.init(function(ret, err) {
	        if(ret.status == 'success'){
	          getRongToken();//获取融云token
	        }else{
	          api.toast({
	          msg: '聊天室初始化失败！请稍后再试',
	          duration: 4000,
	          location: 'bottom'
	        });
	        }
	    });
	};
	function getMsgList(){
			var data = {};
			data['values'] = {};
			data['values']['userId'] = $api.getStorage('userId');
			requestData('Message/getMessage',data,'NO','NO','NO',_callbackMsgList);
	}
	function _callbackMsgList(ret){
			if(ret.status == 200){
					var html = '';
					for(var i=0;i<ret.msgList.length;i++){
							html+='<li class="aui-list-item" onclick="showContent('+i+','+ret.msgList[i].id+')">';
							html+='<div class="aui-list-item-inner aui-list-item-arrow">';
							html+='<div class="aui-list-item-title">'+ret.msgList[i].title+'</div>';
							html+='<div class="aui-list-item-right">';
							if(ret.msgList[i].status == 'notread'){
									html+='<div class="aui-dot" style="position:relative;top:0; right:0" id="msg-dot'+i+'"></div>';
							}else{
									html+='<div class="aui-dot aui-hide" style="position:relative;top:0; right:0" id="msg-dot'+i+'"></div>';
							}
							html+='</div>';
							html+='</div>';
							html+='</li>';
							html+='<li class="aui-list-item aui-hide" id="content'+i+'">'+ret.msgList[i].content+'</li>';
					}
					$api.html($api.byId('list'), html);

			}
	}
	function showContent(i,msgId){
			if($api.cssVal($api.byId('msg-dot'+i),'display') != 'none'){
					var data = {};
					data['values'] = {};
					data['values']['msgId'] = msgId;
					requestData('Message/changeStatus',data,'YES','NO','NO','');
			}
			$api.addCls($api.byId('msg-dot'+i), 'aui-hide');
			if($api.cssVal($api.byId('content'+i),'display') == 'none'){
					$api.removeCls($api.byId('content'+i), 'aui-hide');
			}else{
					$api.addCls($api.byId('content'+i), 'aui-hide');
			}
	}
	function getRongToken(){
    var data = {};
    data['values'] = {};
    data['values']['userId'] = userId;
    data['values']['userName'] = userName;
    data['values']['userHeader'] = userHeader;
    requestData('Chatroom/getToken',data,'YES','NO','NO',_callbackToken);
  }
  function _callbackToken(ret){
    if(ret.code == 200){
      isConnected();
      rong.connect({
          token: ret.token
        },function(ret, err) {
          if(ret.status == 'success'){

          }else{
            api.toast({
            msg: '连接聊天服务器失败！请稍后再试',
            duration: 4000,
            location: 'bottom'
          });
          }
      });
    }else{
      api.toast({
        msg: '获取token失败！请稍后再试',
        duration: 4000,
        location: 'bottom'
      });
    }
  }
  function isConnected(){
    rong.setConnectionStatusListener(function(ret, err) {
      if(ret.result.connectionStatus == 'CONNECTED'){
				getConversationList();
        setOnReceiveMessageListener();
      }
    });
  }
	//获取会话列表
	function getConversationList(){
		rong.getConversationList(function(ret, err) {
	    if(ret.status == 'success'){
				var html = '';
				for (var i in ret.result) {
					var rs = ret.result[i];
					var extra = transExtra(rs.latestMessage.extra);
					if (extra.xm1 == $api.getStorage('userName')) {
						var xm = extra.xm2;
						var tx = extra.tx2;
					} else {
						var xm = extra.xm1;
						var tx = extra.tx1;
					}
					if (rs.conversationType == 'PRIVATE') {
						if (rs.objectName == 'RC:TxtMsg') {
							xx = rs.latestMessage.text.replace(reg, function(a, b) {
								return face[a] ? face[a] : a;
							});
						} else if (rs.objectName == 'RC:ImgMsg') {
							var xx = '[图像消息]'
						} else if (rs.objectName == 'RC:VcMsg') {
							var xx ='[语音消息]'
						}
						html+='<li class="aui-list-item aui-list-item-middle" onclick="lt(' + rs.targetId + ',' + "'" + xm + "'" + ',' + "'" + tx + "'" + ')">';
						if (rs.unreadMessageCount > 0 && extra.xm2 == $api.getStorage('userName')) {
							if(rs.unreadMessageCount<100){
								html+='<div class="aui-badge" style="left:90%">' + rs.unreadMessageCount + '</div>';
							}else{
								html+='<div class="aui-badge" style="left:90%">99+</div>';
							}
						} else {
						html+= '';
						}
						html+='<div class="aui-media-list-item-inner">';
						html+='<div class="aui-list-item-media" style="width: 3rem;">';
						html+='<img src="'+tx+'" class="aui-img-round" >';
						html+='</div>';
						html+='<div class="aui-list-item-inner aui-list-item-arrow">';
						html+='<div class="aui-list-item-text">';
						html+='<div class="aui-list-item-title aui-font-size-14">'+xm+'</div>';
						html+='<div class="aui-pull-right">';

						html+='</div>';

						html+='</div>';
						html+='<div class="aui-list-item-text">';
						html+='<div class="aui-list-item-left">'+xx+'</div>';
						html+='<div class="aui-list-item-right">'+sj(rs.sentTime)+'</div>';
						html+='</div>';
						html+='</div></div>';

						html+= '</li>'
					}
				}
				$api.html($api.byId('messageList'), html);

			}
		})
	}
  function setOnReceiveMessageListener(){
    rong.setOnReceiveMessageListener(function(ret, err) {

    })
  }
	//格式化时间
		function format(now, type) {
			var show_day = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
			var year = now.getFullYear().toString();
			var month = (now.getMonth() + 1).toString();
			var day = (now.getDate()).toString();
			var tian = now.getDay().toString();
			var hour = (now.getHours()).toString();
			var minute = (now.getMinutes()).toString();
			var second = (now.getSeconds()).toString();
			if (hour.length == 1) {
				hour = "0" + hour;
			}
			if (minute.length == 1) {
				minute = "0" + minute;
			}
			if (second.length == 1) {
				second = "0" + second;
			}
			if (type == 1) {
				var dateTime = hour + ":" + minute;
			} else if (type == 2) {
				var dateTime = show_day[tian] + " " + hour + ":" + minute
			} else if (type == 3) {
				var dateTime = year + "-" + month + "-" + day
			} else if (type == 4) {
				var dateTime = year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second;
			} else if (type == 5) {
				var dateTime = show_day[tian];
			}
			return dateTime;
		}
	//时间差计算
		function sj(sj) {
			var nowt = new Date().getTime();
			var a = new Date(parseInt(sj));
			var b = new Date(parseInt(nowt));
			var date1 = Date.parse(format(a, 4));
			var date2 = Date.parse(format(b, 4));
			var xxsj = Math.ceil((date2 - date1) / (60 * 1000))
			if (xxsj <= 1 && xxsj >= 0) {
				return "就刚才";
			} else if (xxsj <= 10 && xxsj > 1) {
				return xxsj + "分钟前";
			} else if (xxsj <= 60 && xxsj > 10) {
				return format(a, 1);
			} else if (xxsj <= 1440 && xxsj > 60) {
				return format(a, 1);
			} else if (xxsj <= 10080 && xxsj > 1440) {
				return format(a, 2);
			} else if (xxsj > 10080) {
				return format(a, 3);
			} else {
				return format(a, 3);
			}
		}
	//json格式转换
		function transExtra(arg) {
			var result = '';
			try {
				result = eval('(' + arg + ')');
			} catch (e) {
				result = arg.slice(1, -1);
			} finally {
			}
			return result;
		}
		//表情符号转换
		var reg = /\[.+?\]/g;
		var face = {
			'[微笑]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_1.png"  width="28"/></span>',
			'[撇嘴]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_2.png"  width="28" /></span>',
			'[色]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_3.png"  width="28" /></span>',
			'[发呆]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_4.png"  width="28" /></span>',
			'[得意]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_5.png"  width="28" /></span>',
			'[流泪]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_6.png"  width="28" /></span>',
			'[害羞]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_7.png"  width="28" /></span>',
			'[闭嘴]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_8.png"  width="28" /></span>',
			'[睡]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_9.png"  width="28" /></span>',
			'[大哭]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_10.png"  width="28"/></span>',
			'[尴尬]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_11.png"  width="28"/></span>',
			'[发怒]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_12.png"  width="28"/></span>',
			'[调皮]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_13.png"  width="28" /></span>',
			'[呲牙]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_14.png"  width="28" /></span>',
			'[惊讶]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_15.png"  width="28" /></span>',
			'[难过]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_16.png"  width="28" /></span>',
			'[酷]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_17.png"  width="28" /></span>',
			'[冷汗]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_18.png"  width="28" /></span>',
			'[抓狂]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_19.png"  width="28" /></span>',
			'[吐]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_20.png"  width="28" /></span>',
			'[偷笑]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_21.png"  width="28" /></span>',
			'[愉快]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_22.png"  width="28" /></span>',
			'[白眼]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_23.png"  width="28" /></span>',
			'[傲慢]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_24.png"  width="28" /></span>',
			'[饥饿]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_25.png"  width="28" /></span>',
			'[困]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_26.png"  width="28" /></span>',
			'[恐惧]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_27.png"  width="28" /></span>',
			'[流汗]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_28.png"  width="28" /></span>',
			'[憨笑]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_29.png"  width="28" /></span>',
			/*从这*/
			'[悠闲]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_30.png"  width="28" /></span>',
			'[奋斗]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_31.png"  width="28" /></span>',
			'[咒骂]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_32.png"  width="28" /></span>',
			'[疑问]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_33.png"  width="28" /></span>',
			'[嘘]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_34.png"  width="28" /></span>',
			'[晕]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_35.png"  width="28" /></span>',
			'[疯了]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_36.png"  width="28" /></span>',
			'[衰]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_37.png"  width="28" /></span>',
			'[骷髅]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_38.png"  width="28" /></span>',
			'[敲打]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_39.png"  width="28"/></span>',
			'[再见]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_40.png"  width="28"/></span>',
			'[擦汗]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_41.png"  width="28"/></span>',
			'[抠鼻]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_42.png"  width="28" /></span>',
			'[鼓掌]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_43.png"  width="28" /></span>',
			'[糗大了]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_44.png"  width="28" /></span>',
			'[坏笑]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_45.png"  width="28" /></span>',
			'[左哼哼]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_46.png"  width="28" /></span>',
			'[右哼哼]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_47.png"  width="28" /></span>',
			'[哈欠]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_48.png"  width="28" /></span>',
			'[鄙视]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_49.png"  width="28" /></span>',
			'[委屈]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_50.png"  width="28" /></span>',
			'[快哭了]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_51.png"  width="28" /></span>',
			'[阴险]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_52.png"  width="28" /></span>',
			'[亲亲]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_53.png"  width="28" /></span>',
			'[吓]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_54.png"  width="28" /></span>',
			'[可怜]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_55.png"  width="28" /></span>',
			'[菜刀]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_56.png"  width="28" /></span>',
			'[西瓜]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_57.png"  width="28" /></span>',
			'[啤酒]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_58.png"  width="28" /></span>',
			'[篮球]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_59.png"  width="28" /></span>',
			'[乒乓]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_60.png"  width="28" /></span>',
			'[咖啡]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_61.png"  width="28" /></span>',
			'[饭]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_62.png"  width="28" /></span>',
			'[猪头]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_63.png"  width="28" /></span>',
			'[玫瑰]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_64.png"  width="28" /></span>',
			'[凋谢]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_65.png"  width="28" /></span>',
			'[嘴唇]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_66.png"  width="28" /></span>',
			'[爱心]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_67.png"  width="28" /></span>',
			'[心碎]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_68.png"  width="28"/></span>',
			'[蛋糕]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_69.png"  width="28"/></span>',
			'[闪电]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_70.png"  width="28"/></span>',
			'[炸弹]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_71.png"  width="28" /></span>',
			'[刀]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_72.png"  width="28" /></span>',
			'[足球]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_73.png"  width="28" /></span>',
			'[瓢虫]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_74.png"  width="28" /></span>',
			'[便便]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_75.png"  width="28" /></span>',
			'[月亮]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_76.png"  width="28" /></span>',
			'[太阳]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_77.png"  width="28" /></span>',
			'[礼物]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_78.png"  width="28" /></span>',
			'[拥抱]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_79.png"  width="28" /></span>',
			'[强]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_80.png"  width="28" /></span>',
			'[弱]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_81.png"  width="28" /></span>',
			'[握手]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_82.png"  width="28" /></span>',
			'[胜利]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_83.png"  width="28" /></span>',
			'[抱拳]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_84.png"  width="28" /></span>',
			'[勾引]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_85.png"  width="28" /></span>',
			'[拳头]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_86.png"  width="28" /></span>',
			'[差劲]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_87.png"  width="28" /></span>',
			'[爱你]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_88.png"  width="28" /></span>',
			'[NO]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_89.png"  width="28" /></span>',
			'[OK]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_90.png"  width="28" /></span>',
			'[爱情]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_91.png"  width="28" /></span>',
			'[飞吻]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_92.png"  width="28" /></span>',
			'[跳跳]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_93.png"  width="28" /></span>',
			'[发抖]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_94.png"  width="28" /></span>',
			'[怄火]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_95.png"  width="28" /></span>',
			'[转圈]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_96.png"  width="28" /></span>',
			'[磕头]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_97.png"  width="28"/></span>',
			'[回头]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_98.png"  width="28"/></span>',
			'[跳绳]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_99.png"  width="28"/></span>',
			'[投降]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_100.png"  width="28" /></span>',
			'[激动]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_101.png"  width="28" /></span>',
			'[街舞]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_102.png"  width="28" /></span>',
			'[献吻]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_103.png"  width="28" /></span>',
			'[左太极]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_104.png"  width="28" /></span>',
			'[右太极]' : '<span style="display:inline-block"><img src="../res/ChatBox/emotion/Expression_105.png"  width="28" /></span>'
		};
		function lt(id, xm, tx) {
			var data = {};
			data['id'] = id;
			data['user_name'] = xm;
			data['user_header'] = tx;
			api.openWin({
					name: 'chatRoom',
					url: './winChatroom.html',
					pageParam: {
							page:'chatRoom',
							title: '联系卖家',
							data:data
					}
			});
			if ($api.getStorage('userId') == id) {
			} else {
				rong.clearMessagesUnreadStatus({
					conversationType : 'PRIVATE',
					targetId : id
				}, function(ret, err) {
				})
			}
		}
</script>
</html>
